<%@ page import="cz.tpj.logic.*" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="cz.tpj.database.mesto.*" %>
<%@ page import="cz.tpj.database.bod_zaujmu.*" %>
<%@ page import="cz.tpj.database.obrazok_mesto.*" %>
<%@ page import="cz.tpj.database.obrazky.*" %>
<%@ page import="cz.tpj.upl.Temporary"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%
final String ACTION_SELECTED = "selected_city";
final String SEARCH_CITY = "search_from_city";
//String contextPath = (config.getServletContext()).getContextPath();
final String THIS = Navigation.JSP_CULTURAL_INTERESTS;
//inicializace
Table_city tableCity = Table_city.getInstance();
Table_CityPictures tableCityPicture= Table_CityPictures.getInstance();
%>
<html>
    <%= Navigation.getHead(null) %>
    <%= Navigation.getStartBody() %>
    <%= Navigation.getNavigationPanel(Navigation.JSP_CULTURAL_INTERESTS, request) %>
        <div id="<%= Navigation.DIV_CENTER %>">
            <h1>Města</h1>
            <p>Vyber město:</p>
            <!--********************vyhledavaci formlar**************************-->
            <form method="POST" action=<%= Navigation.JSP_CULTURAL_INTERESTS %>>
                <select  name="<%= ACTION_SELECTED%>">
                    <%
                    //nacte id posledniho zvoleneho mesta
                    String lastSelected = request.getParameter(ACTION_SELECTED);
                    //promenna pro jednotliva mesta
                    City city;
                    //ziska vsecky mesta v databazi
                    ArrayList<City> allCityes = tableCity.getAllCity();
                    Iterator<City> itCityes = allCityes.iterator();
                    //prochaze jednotlivych mest
                    while(itCityes.hasNext()){
                        //nacte jedno mesto
                        city = itCityes.next();
                        //zjisti jeho id a pretipuje na String
                        String idThisCity = city.getId();
                        //zjsiti jestli prave nactene mesto neni to co bylo naposled zvoleno
                        //1) bylo -> prida paramatr "selected"
                        //2) nebylo -> bez selected
                        if(idThisCity.equals(lastSelected) ){
                            out.println("<option selected value='"+city.getId()+"'>"+city.getName()+"</option> ");
                        }else{
                            out.println("<option value='"+city.getId()+"'>"+city.getName()+"</option> ");
                        }
                    }

                    %>
                </select>
                <input type="hidden" name="<%= Navigation.LINK%>" value="<%= THIS%>"/>
                <input type="submit" name="<%=SEARCH_CITY%>" value="najdi město"/>
            </form>
            <!-- ****************zobrazi se jen pokud bylo zadano vyhledani mesta****************** -->
            <hr width="470" />
            <%
            String parSelecteSity = request.getParameter(SEARCH_CITY);
            if(parSelecteSity != null){
                //najde jmeno mesta
                String cityId = request.getParameter(ACTION_SELECTED);
                City selectedCity = tableCity.getCityNameFromId(cityId);
                //najde zajimavosti o meste
                Table_Bod_zaujmu tableBod = Table_Bod_zaujmu.getInstance();
                ArrayList<Bod_zaujmu> allInterests = tableBod.getCulturalCityInterests(cityId);
                //Iterator<Bod_zaujmu> itInterests = allInterests.iterator();
                //nacte obrazky do tempu (najdeje podle id mesta)
                String source = Navigation.MEDIA_FOLDER_CITIES + "/" + cityId + "/";
				String destination = Navigation.ABSOLUT_SERVER_TEMP_CITIES_FOLDER;
				Temporary.getInstance().addFolderInTemp(source, destination);
                //-------------vypis udaju---------
                %>
                <h1><%= selectedCity.getName()%></h1>
                <p>Krátký popis:<br/><%= selectedCity.getshortDescription()%></p>
                <p>Dlouhý popis:<br/><%= selectedCity.getlongDescription() %></p>
                <%                
                //-------------najde fotky-----------
                ArrayList<Obrazok> obr = tableCityPicture.getPictureFromCityId(cityId);
                Iterator<Obrazok> itObr = obr.iterator();
                String obrPath;
                String path;
                Obrazok obrazok;
                //zobrazi kazdou jednu fotku co nasel
                while(itObr.hasNext()){
                    obrazok = itObr.next();
                    path = Navigation.TEMP_CITYES + obrazok.getCesta();
                    %>
                    <img src="<%= path%>" width="200"/>
                    <%
                }
            }else{
                %>
                <p>Nahoře si vyberte město o kterém vás zajímají kulturní zajímavosti.</p>
                <%
            }
            %>
            <hr width="470" />
        </div>
        <%= Navigation.getLoginLink(request) %>
</html>
